import axios from 'axios'
import { Message } from 'element-ui'
import qs from 'qs'
import store from '@/store'
// import store from '@/store'

const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
  // baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
  // withCredentials: true, // send cookies when cross-domain requests
  timeout: 5000 // request timeout
})
// 设置请求次数，请求的间隙
service.defaults.retry = 4;
service.defaults.retryDelay = 1000;

// request interceptor
service.interceptors.request.use(
  config => {
    // do something before request is sent
    const projectId = sessionStorage.projectId

    if (config.data && config.data.constructor === Object) {
      config.data['projectId'] = projectId
      config.data['projectid'] = projectId
    } else if (config.params && config.params.constructor === Object) {
      config.params['projectId'] = projectId
      config.params['projectid'] = projectId
    }

    if (config.qs) {
      config.method === 'post'
        ? (config.data = qs.stringify({
          ...config.data
        }))
        : (config.params = {
          ...config.params
        })
      config.headers['Content-Type'] =
        'application/x-www-form-urlencoded;charset=utf-8'
    } else {
      config.headers['Content-Type'] = config.headers['Content-Type']
        ? config.headers['Content-Type']
        : 'application/json;charset=UTF-8'
    }

    return config
  },
  error => {
    // do something with request error
    return Promise.reject(error)
  }
)

// response interceptor
service.interceptors.response.use(
  /**
   * If you want to get http information such as headers or status
   * Please return  response => response
   */

  /**
   * Determine the request status by custom code
   * Here is just an example
   * You can also judge the status by HTTP Status Code
   */
  response => {
    const res = response.data
    // if the custom code is not 20000, it is judged as an error.
    // if (response.status !== 200) {
    if (res.result !== 'SUCCESS' && res.status !== 'SUCCESS' && response.config.url.indexOf('/file/note/get') < 0 && response.config.url.indexOf('/file/note/picture/add') < 0 && response.config.url.indexOf('/file/note/get') < 0 && response.config.url.indexOf('/file/version/get') < 0) {
      Message({
        message: res.msg || 'Error',
        type: 'error',
        duration: 5 * 1000
      })
    } else {
      return res
    }
  },
  error => {
    // console.log(error.request)
    // console.log(error.message)
    // console.log(error.config)
    // console.log(error.response)
    console.log(error)
    if (error.response.data === 'unauthorized') {
      Message({
        message: '登录超时',
        type: 'error',
        duration: 5 * 1000
      })
      store.dispatch('index/logout')
    } else {
      Message({
        message: error.msg,
        type: 'error',
        duration: 5 * 1000
      })
    }

    return Promise.reject(error)
  }
)

export default service
